﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace InputProductForm
{
    public partial class frmAddQuality : UTLBASEFORM.MDICHILD
    {
        public frmAddQuality()
        {
            InitializeComponent();
        }
        DataTable data = new DataTable();
        TQuality tQuality = new TQuality();
        int id = 0;
        string name = "";
        string sym = "";
        bool active = true;
        private void SetupMenuBar()
        {
            ShowBTN1(TextLabelAdd, ImageNameCheck_Ok);
            ShowBTN2(TextLabelEdit, ImageNameEdit);
            ShowBTN3("Hủy", ImageNameCancel);

        }
        private void LoadQuatily()
        {

            data = tQuality.GetQuality();
            lv.Items.Clear();
            string active;
            for (int i = 0; i < data.Rows.Count; i++)
            {
                int stt = i + 1;
                ListViewItem item = new ListViewItem(stt.ToString());
                lv.Items.Add(item);
                item.SubItems.Add(data.Rows[i]["Name"].ToString());
                item.SubItems.Add(data.Rows[i]["Symbol"].ToString());
                if (data.Rows[i]["Active"].ToString() == "True")
                    active = "Hoạt động";
                else active = "Khóa";
                item.SubItems.Add(active);

                if (i % 2 == 0)
                {
                    item.BackColor = Color.AliceBlue;
                    //return ;
                }
                else
                {
                    item.BackColor = Color.AntiqueWhite;
                    //return Brushes.Beige;
                }

            }

        }
        public void SetInfo()
        {
            name = txtName.Text;
            sym = txtSymbol.Text;
            if (cmbActive.SelectedText.Trim() == "Khóa")
                active = false;
            else active = true;
        }
        public override void btn1_Click(object sender, EventArgs e)
        {
            SetInfo();
            tQuality.Insert(name,sym, active);
            LoadQuatily();
            RefeshForm();
        }
        public void UpdateQuality()
        {
            SetInfo();
            tQuality.Update(id, name,sym, active);
            LoadQuatily();
            // RefeshForm();
        }
        public override void btn2_Click(object sender, EventArgs e)
        {
            UpdateQuality();
            ChangeButton("Insert");
        }
        public override void btn3_Click(object sender, EventArgs e)
        {
            ChangeButton("Insert");
        }
        private void ChangeButton(string state)
        {
            if (state == "Insert")
            {
                HideBTN3();
                HideBTN2();
                ShowBTN1(TextLabelAdd, ImageNameCheck_Ok);
                RefeshForm();
            }
            else if (state == "Update")
            {

                HideBTN1();
                ShowBTN2(TextLabelEdit, ImageNameEdit);
                ShowBTN3("Hủy", ImageNameCancel);

            }
        }
        public void RefeshForm()
        {
            txtName.Text = "";
        }
        public void ShowListToForm(int _id)
        {
            id = int.Parse(data.Rows[_id]["ID"].ToString());
            txtName.Text = data.Rows[_id]["Name"].ToString();
            txtSymbol.Text = data.Rows[_id]["Symbol"].ToString();
            if (data.Rows[_id]["Active"].ToString() == "True")
                cmbActive.Text = "Hoạt động";
            else cmbActive.Text = "Khóa";
        }



        private void frmAddQuality_Load(object sender, EventArgs e)
        {
            SetupMenuBar();
            LoadQuatily();
            ChangeButton("Insert");
        }

        private void lv_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeButton("Update");
            ListView lv1 = (ListView)sender;

            if (lv1.Items.Count > 0)
            {
                int flag = -1;
                for (int i = 0; i < lv1.Items.Count; i++)
                {
                    if (lv1.Items[i].Selected)
                    {
                        flag = i;
                    }
                }
                if (flag != -1)
                {

                    ShowListToForm(flag);

                }
            }
        }
    }
}
